package com.hui.servlet;

import com.hui.entity.Student;
import com.hui.service.StudentService;
import com.hui.util.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/06/20/11:37
 * @Description: 分页获取学员信息 ，每页显示10条数据
 */
@WebServlet(name = "PageStudentServlet", urlPatterns = "/pageStudent")
public class PageStudentServlet extends HttpServlet {
    private StudentService studentService = ServiceFactory.getStudentService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> studentList ;
        // 获取分页数据
        String page = req.getParameter("page");
        if (page != null && Integer.parseInt(page) > 0) {
            int i = Integer.parseInt(page);
            studentList = studentService.getPageStudent(i);
        } else { // 默认第一页
            studentList = studentService.getPageStudent(1);
        }
        HttpSession session = req.getSession();
        // 将总页数保存到session中
        int totalPage = studentService.getTotalPage();
        session.setAttribute("totalPage", totalPage);
        // 将获取的学生数据保存到session中
        session.setAttribute("studentList", studentList);
        // 响应
        resp.sendRedirect("home.jsp");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
